﻿using KINLO.UI.Main.Hepler;
using KINLO.UI.Main.MES;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace KINLO.UI.Main
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            Console.WriteLine(e);
            MessageBox.Show(e.Exception.Message);
            e.Handled = true;
        }
        Mutex mutex;
        protected override void OnStartup(StartupEventArgs e)
        {

            bool testResult = false;
            mutex = new Mutex(true, "KINLO", out testResult);
            if (testResult)
            {
                base.OnStartup(e);
                AlarmInfosInit();
            }
            else
            {
                MessageBox.Show("程序已运行,请勿重复运行", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                Environment.Exit(0);
                return; ;
            }
        }
        /// <summary>
        /// 报警信息加载初始化
        /// </summary>
        private void AlarmInfosInit()
        {
            try
            {
                var alarmInfosClass = AlarmInfosData.Instance;

                NPOIHandler npoiHandler = new NPOIHandler();

                //var alarmInfosData = new Dictionary<string, List<AlarmInfo>>();
                //alarmInfosData.Add("测短路", npoiHandler.ParseExcel("测短路.xls"));
                //alarmInfosData.Add("点底焊", npoiHandler.ParseExcel("点底焊.xls"));
                //alarmInfosData.Add("盖帽焊120", npoiHandler.ParseExcel("盖帽焊120.xls"));
                //alarmInfosData.Add("滚槽机", npoiHandler.ParseExcel("滚槽机.xls"));
                //alarmInfosData.Add("换套杯", npoiHandler.ParseExcel("换套杯.xls"));
                //alarmInfosData.Add("卷芯上料", npoiHandler.ParseExcel("卷芯上料.xls"));
                //alarmInfosData.Add("入壳机", npoiHandler.ParseExcel("入壳机.xls"));
                //alarmInfosData.Add("套膜机120", npoiHandler.ParseExcel("套膜机120.xls"));
                //alarmInfosData.Add("折极耳", npoiHandler.ParseExcel("折极耳.xls"));
                //alarmInfosData.Add("装盘机", npoiHandler.ParseExcel("装盘机.xls"));

                var alarmInfosData = new Dictionary<string, List<AlarmInfo>>
                {
                    { "注液机", npoiHandler.ParseExcel("18线注液机.xls") }


                };
                alarmInfosClass.WriteExcelData(alarmInfosData);
            }
            catch (Exception ex)
            {
                // 在这里处理异常，您可以使用消息框或记录到日志文件中
                System.Windows.MessageBox.Show("加载报警信息时发生错误：" + ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                // 或者使用日志记录工具记录异常信息
                // Logger.Log(ex);
            }
        }
    }

}
